class Fraction:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def iterate(self):
        num = self.a * 10 // self.b
        tal = self.a * 10 % self.b
        return num, Fraction(tal, self.b)

    def __eq__(self, f):
        return self.a==f.a and self.b==f.b

    def __ne__(self, f):
        return not self==f

    def __hash__(self):
        return (self.a+self.b)*(self.a+self.b+1)//2 + self.b

class Solution(object):
    def fractionToDecimal(self, numerator, denominator):
        """
        :type numerator: int
        :type denominator: int
        :rtype: str
        """
        if numerator * denominator < 0:
            numerator = abs(numerator)
            denominator = abs(denominator)
            result = "-{}".format(numerator//denominator)
        else:
            numerator = abs(numerator)
            denominator = abs(denominator)
            result = "{}".format(numerator//denominator)
        numerator = numerator % denominator
        if numerator==0:
            return result
        else:
            result += "."
        process = set()
        f = Fraction(numerator, denominator)
        while f not in process and f.a != 0:
            process.add(f)
            n, f = f.iterate()
            result += "{}".format(n)
        if f in process:
            loop = ""
            process.clear()
            while f not in process:
                process.add(f)
                n, f = f.iterate()
                loop += "{}".format(n)
            result = result[:len(result)-len(loop)] + "({})".format(loop)
        return result

print(Solution().fractionToDecimal(-50,8))